
import 'package:flutter/material.dart';

class MyAnimation extends StatefulWidget {
  @override
  _MyAnimationState createState() => _MyAnimationState();
}

class _MyAnimationState extends State<MyAnimation>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  int _num = 0;
  int _numAdd = 0;

  String firBgImage = 'assets/images/calendar_spring.png';
  String secBgImage = 'assets/images/calendar_spring.png';
  bool isShowMoney = true;

  int mounth = 1;

  final Map<String, Map<String, dynamic>> seasonList = {
    ///春
    '3': {
      'color': Color(0xfffc5765),
      'bgIcon': 'assets/images/calendar_spring.png'
    },
    '4': {
      'color': Color(0xfffc5765),
      'bgIcon': 'assets/images/calendar_spring.png'
    },
    '5': {
      'color': Color(0xfffc5765),
      'bgIcon': 'assets/images/calendar_spring.png'
    },

    ///夏
    '6': {
      'color': Color(0xff00c4c7),
      'bgIcon': 'assets/images/calendar_summer.png'
    },
    '7': {
      'color': Color(0xff00c4c7),
      'bgIcon': 'assets/images/calendar_summer.png'
    },
    '8': {
      'color': Color(0xff00c4c7),
      'bgIcon': 'assets/images/calendar_summer.png'
    },

    ///秋
    '9': {
      'color': Color(0xffff8011),
      'bgIcon': 'assets/images/calendar_autumn.png'
    },
    '10': {
      'color': Color(0xffff8011),
      'bgIcon': 'assets/images/calendar_autumn.png'
    },
    '11': {
      'color': Color(0xffff8011),
      'bgIcon': 'assets/images/calendar_autumn.png'
    },

    ///冬
    '12': {
      'color': Color(0xff1957a1),
      'bgIcon': 'assets/images/calendar_winter.png'
    },
    '1': {
      'color': Color(0xff1957a1),
      'bgIcon': 'assets/images/calendar_winter.png'
    },
    '2': {
      'color': Color(0xff1957a1),
      'bgIcon': 'assets/images/calendar_winter.png'
    },
    '--': {
      'color': Color(0xff1957a1),
      'bgIcon': 'assets/images/calendar_winter.png'
    },
  };

  var infoListMes = [
    {'title': 'BMI', 'value': '你看嘛'},
    {'title': '劳动强度', 'value': '1'},
    {'title': '血糖(mmol)', 'value': '5'},
    {'title': 'HbAIC', 'value': '5%'},
  ];
  @override
  void initState() {
    int a = 3;
    int b = 5;
    int c = 8;
    double e = 11.11111;
    print(e ~/ b);

    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('动画'),
      ),
      body: _bodyWidget(),
      floatingActionButton: FloatingActionButton(onPressed: () {
        _numAdd++;
        setState(() {
          _num++;

          if (_num > 3) {
            _num = 1;
          }
        });
      }),
    );
  }

  Widget _bodyWidget() {
    return AnimatedPadding(
      duration: Duration(seconds: 2),
      padding: EdgeInsets.all(20.0 * _num),
      child: Column(
        children: [
          Row(
            children: [
              AnimatedContainer(
                duration: Duration(seconds: 1),
                curve: Curves.bounceOut,
                width: 70,
                height: 80.0 * _num * 0.5,
                color: Colors.red,
                child: AnimatedSwitcher(
                    transitionBuilder: (child, animation) {
                      return FadeTransition(
                        opacity: animation,
                        child: child,
                      );
                    },
                    duration: Duration(seconds: 2),
                    child: Text(
                      _num.toString() * 5,
                      key: UniqueKey(),
                      style: TextStyle(fontSize: 20),
                    )),
              ),
              SizedBox(
                width: 10,
              ),
              AnimatedContainer(
                duration: Duration(seconds: 1),
                width: 70,
                height: 80.0,
                color: Colors.red,
                child: AnimatedSwitcher(
                    transitionBuilder: (child, animation) {
                      return FadeTransition(
                        opacity: animation,
                        child: ScaleTransition(
                          scale: animation,
                          child: child,
                        ),
                      );
                    },
                    duration: Duration(seconds: 2),
                    child: Text(
                      _num.toString() * 5,
                      key: UniqueKey(),
                      style: TextStyle(fontSize: 20),
                    )),
              ),
              SizedBox(
                width: 10,
              ),
              AnimatedOpacity(
                duration: Duration(seconds: 1),
                opacity: _num * 0.3,
                child: Container(
                  width: 50,
                  height: 80.0,
                  color: Colors.indigo,
                  child: AnimatedSwitcher(
                      transitionBuilder: (child, animation) {
                        return FadeTransition(
                          opacity: animation,
                          child: ScaleTransition(
                            scale: animation,
                            child: child,
                          ),
                        );
                      },
                      duration: Duration(seconds: 2),
                      child: Text(
                        _num.toString() * 5,
                        key: UniqueKey(),
                        style: TextStyle(fontSize: 20),
                      )),
                ),
              ),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Row(
            children: [
              TweenAnimationBuilder(
                  tween: Tween<double>(begin: 0, end: _num * 0.4),
                  duration: Duration(seconds: 2),
                  builder: (BuildContext context, value, Widget child) {
                    return Opacity(
                      opacity: value > 1 ? 1 : value,
                      child: Container(
                        color: Colors.cyan,
                        width: 100,
                        height: 100,
                        child: Center(
                          child: Row(
                            children: [
                              //旋转 0-π
                              Transform.rotate(
                                angle: value * 2,
                                child: Text(
                                  '旋转',
                                  // style: TextStyle(fontSize: value * 15),
                                ),
                              ),
                              //缩放
                              Transform.scale(
                                scale: value * 1.0,
                                child: Text(
                                  '缩放',
                                  // style: TextStyle(fontSize: value * 15),
                                ),
                              ),
                            ],
                          ),
                        ),
                        // child: buildPersonInfo(infoListMes)
                      ),
                    );
                  }),
              SizedBox(
                width: 10,
              ),
              TweenAnimationBuilder(
                  tween: Tween(
                      begin: Offset(0.0, -10.0),
                      end: Offset(20.0, _num * 14.0)),
                  duration: Duration(seconds: 2),
                  builder: (BuildContext context, value, Widget child) {
                    return Container(
                      color: Colors.cyan,
                      width: 100,
                      height: 100,
                      child: Center(
                        //平移
                        child: Transform.translate(
                          offset: value,
                          child: Text(
                            '平移',
                            // style: TextStyle(fontSize: value * 15),
                          ),
                        ),
                      ),
                      // child: buildPersonInfo(infoListMes)
                    );
                  }),
            ],
          ),
          SizedBox(
            height: 10,
          ),
          Container(
            width: 300,
            height: 160,
            color: Colors.red,
            child: Counter(numAdd: _numAdd),
          ),
          SizedBox(
            height: 10,
          ),
          AnimatedContainer(
            width: 100,
            height: 100,
            duration: Duration(seconds: 1),
            child: Center(child: Text(_num.toString())),
            decoration: BoxDecoration(
                gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    stops: [0.1 * _num, 0.8],
                    colors: [Colors.green, Colors.pink]),
                boxShadow: [BoxShadow(spreadRadius: 1, blurRadius: 1)],
                borderRadius: BorderRadius.circular(100)),
          ),
          GestureDetector(
            // behavior: HitTestBehavior.opaque,
            child: Container(
                padding: EdgeInsets.only(left: 3),
                child: AnimatedCrossFade(
                  firstChild:
                      //    Image.asset(
                      //   seasonList[_monthsList.length == 6
                      //       ? (_monthsList[_selectedIndex]?.month?.toString() ?? '--')
                      //       : '1']['bgIcon'],
                      //   width: double.infinity,
                      //   // height: 20,
                      //   fit: BoxFit.fitWidth,
                      // ),
                      Image.asset(firBgImage,
                          width: 360, height: 100, fit: BoxFit.fitWidth),
                  secondChild: Image.asset(secBgImage,
                      width: 360, height: 120, fit: BoxFit.fitWidth),
                  crossFadeState: isShowMoney
                      ? CrossFadeState.showFirst
                      : CrossFadeState.showSecond,
                  sizeCurve: Curves.easeInBack,
                  duration: Duration(milliseconds: 500),
                )),
            onTap: () {
              isShowMoney = !isShowMoney;

              mounth += 3;
              if (mounth > 10) {
                mounth = 1;
              }
              if (!isShowMoney) {
                firBgImage = secBgImage;
                secBgImage = seasonList[mounth.toString()]['bgIcon'];
              } else {
                secBgImage = firBgImage;
                firBgImage = seasonList[mounth.toString()]['bgIcon'];
              }

              setState(() {});
            },
          ),
        ],
      ),
    );
  }

  ///创建用户信息
  Widget buildPersonInfo(List list) {
    return Row(
      children: List.generate(list.length, (index) {
        var item = list[index];
        return Expanded(
          child: Container(
            child: Column(
              textBaseline: TextBaseline.alphabetic,
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(top: 22, bottom: 18),
                  child: Text(
                    item['title'],
                    style: TextStyle(
                      fontSize: 20,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
                Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(bottom: 20),
                  child: Text(
                    item['value'],
                    style: TextStyle(
                      fontSize: 28,
                      fontWeight: FontWeight.w500,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
          ),
        );
      }),
    );
  }
}

class Counter extends StatelessWidget {
  const Counter({
    Key key,
    @required int numAdd,
  })  : _numAdd = numAdd,
        super(key: key);

  final int _numAdd;

  @override
  Widget build(BuildContext context) {
    return TweenAnimationBuilder(
      tween: Tween<double>(end: _numAdd.toDouble()),
      builder: (BuildContext context, value, Widget child) {
        int whole = value ~/ 1;
        double decimal = value - whole;
        // print('$whole,....,$decimal');
        return Center(
          child: Stack(
            children: [
              Positioned(
                  top: -160 * decimal,
                  child: Opacity(
                    opacity: 1.0 - decimal,
                    child: Text(
                      whole.toString(),
                      style: TextStyle(fontSize: 150),
                    ),
                  )),
              Positioned(
                  top: 160 - 160 * decimal,
                  child: Opacity(
                    opacity: decimal,
                    child: Text(
                      (whole + 1).toString(),
                      style: TextStyle(fontSize: 150),
                    ),
                  ))
            ],
          ),
        );
      },
      duration: Duration(seconds: 1),
    );
  }
}
